﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ENG.EngStartQueueLib;

namespace ENG.EngStartQueue
{
  public partial class CtrRefListView : UserControl
  {
    public CtrRefListView()
    {
      InitializeComponent();
    }

    public void Add(List<Reference> lst)
    {
      foreach (var fItem in lst)
      {
        ListViewItem item = new ListViewItem();
        item.Text = fItem.FileName;
        item.SubItems.Add(fItem.Path);
        item.SubItems.Add(fItem.Arguments);
        item.SubItems.Add(fItem.Source);
        item.Tag = fItem;

        lvw.Items.Add(item);
      } // foreach (var fItem in lst)

      AutoResizeColumns();
    }
    public void Set(List<Reference> lst)
    {
      lvw.Items.Clear();
      Add(lst);
    }
    public void AutoResizeColumns()
    {
      int[] w = new int[lvw.Columns.Count];

      lvw.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
      for (int i = 0; i < lvw.Columns.Count; i++)
      {
        w[i] = lvw.Columns[i].Width;
      }
      lvw.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
      for (int i = 0; i < lvw.Columns.Count; i++)
      {
        w[i] = Math.Max(w[i], lvw.Columns[i].Width);
      }

      for (int i = 0; i < lvw.Columns.Count; i++)
      {
        lvw.Columns[i].Width = w[i];
      }
    }

    public List<Reference> GetSelectedItems()
    {
      List<Reference> ret = new List<Reference>();

      foreach (ListViewItem fItem in lvw.SelectedItems)
      {
        ret.Add(
          (Reference)fItem.Tag);
      } // foreach (ListViewItem fItem in lvw.SelectedItems)

      return ret;
    }
  }
}
